package com.gcd.alienandwheel.stage;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.actors.Label;
import com.gcd.alienandwheel.MyGame;
import com.gcd.alienandwheel.actor.Bar;
import com.gcd.alienandwheel.actor.Track;
import com.gcd.alienandwheel.actor.Wheel;

/**
 *
 * @author Rezan Achmad
 */
public class Arena extends MyStage {

    float lastX, lastY;
    Bar barVelocity;
    float velocity = 0;
    float tempAccY, tempAccX, maxVelocity;
    boolean press = false;
    Label lblVelocity;
    Track track;
    Wheel wheel;
    int rotate = 0;
    Vector2 v = new Vector2(0, 0);
    int acc = 1;

    public Arena(MyGame myGame, float width, float height, boolean stretch) {
        super(myGame, width, height, stretch);
        init();
    }

    private void init() {
        maxVelocity = 20;

        track = new Track("track");
        addActor(track);

        barVelocity = new Bar("bar", maxVelocity, width, 30, Color.WHITE, true);
        barVelocity.setValue(height);
        addActor(barVelocity);

        font.setColor(1, 0, 0, 1);
        lblVelocity = new Label("lblVelocity", font, "0");
        lblVelocity.y = 100;
        addActor(lblVelocity);

        wheel = new Wheel("wheel");
        addActor(wheel);
        wheel.x = (width - wheel.width) / 2;
        wheel.y = (height - wheel.height) / 2;
    }

    @Override
    public boolean touchDown(int x, int y, int pointer, int newParam) {
        Vector2 out = new Vector2();
        toStageCoordinates(x, y, out);

        lastX = out.x;
        lastY = out.y;
        press = true;

        return super.touchDown(x, y, pointer, newParam);
    }

    @Override
    public boolean touchUp(int x, int y, int pointer, int button) {
        press = false;
        return super.touchUp(x, y, pointer, button);
    }

    @Override
    public boolean touchDragged(int x, int y, int pointer) {
        Vector2 out = new Vector2();
        toStageCoordinates(x, y, out);

        tempAccX = (out.x - lastX) / Gdx.graphics.getDeltaTime();
        tempAccY = (out.y - lastY) / Gdx.graphics.getDeltaTime();
        tempAccY /= height / 2;

        lastX = out.x;
        lastY = out.y;

        return super.touchDragged(x, y, pointer);
    }

    @Override
    public void draw() {
//        float x = wheel.x - (width - wheel.width) / 2;
//        float y = wheel.y - (height - wheel.height) / 2;
//        projection.setToOrtho2D(x, y, width, height);

        super.draw();
    }

    @Override
    public void act(float delta) {
        int inc = 1;
        Vector2 _v = new Vector2(v);

        if (Gdx.input.isKeyPressed(Keys.KEYCODE_A)) {
//            wheel.rotation += inc;
//            track.rotation -= inc;
            rotate = -1;
            if (rotate < -3) {
                rotate = -3;
            }
        } else 

        if (Gdx.input.isKeyPressed(Keys.KEYCODE_D)) {
//            wheel.rotation -= inc;
//            track.rotation += inc;
            rotate = 1;
            if (rotate > 3) {
                rotate = 3;
            }
        }
        else {
            rotate = 0;
        }

        if (Gdx.input.isKeyPressed(Keys.KEYCODE_W)) {
//            wheel.y += v.y;
//            wheel.x += v.x;
            track.rotation += rotate * delta;
            _v.rotate(-(track.rotation));
            track.y -= _v.y;
            track.x -= _v.x;
            v.y += acc * delta;
        } else if (Gdx.input.isKeyPressed(Keys.KEYCODE_S)) {
//            wheel.y -= v.y;
//            wheel.x -= v.x;
            track.rotation -= rotate * delta;
            _v.rotate(-(track.rotation));
            track.y += _v.y;
            track.x += _v.x;
            v.y += acc * delta;
        } else {
            v.x = 0;
            v.y = 0;
        }

        track.originX = -(track.x - (width - wheel.width) / 2);
        track.originY = -(track.y - (height - wheel.height) / 2);

        // Velocity of y is minus
//        if (tempAccY < 0) {
//            velocity += tempAccY;
//        } else {
//            velocity = Math.max(velocity, tempAccY);
//        }

        // Decrement Velocity
        velocity += ((-1.5 + tempAccY) * delta);

        // Validate Velocity
        if (velocity > maxVelocity) {
            velocity = maxVelocity;
        } else if (velocity < 0) {
            velocity = 0;
        }

        // Set barVelocity value
        barVelocity.setValue(velocity);

        // Debug, set to label
        lblVelocity.text = Float.toString(velocity);

        // Reset velocity
        tempAccY = 0;

        super.act(delta);
    }

    public void debug(String text) {
        if (press) {
            System.out.println(text);
        }
    }

    public void debug(int n) {
        debug(Integer.toString(n));
    }

    public void debug(float n) {
        debug(Float.toString(n));
    }
}
